package com.zhiyou100.controller;

import com.zhiyou100.pojo.User;
import com.zhiyou100.service.UserService;
import com.zhiyou100.test.Email;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Controller
public class UserController {
    @Autowired
    @Qualifier("userServiceImpl")
//    @Resource
    private UserService userService;

    @RequestMapping("/toIndex.do")
    public String toIndex(String name,String password){
        System.out.println(name+";"+password);
        System.out.println("toIndex");
        return "index";
    }
    @RequestMapping("/toUser.do")
    public String toUser(User user, Model model){
        List<User> users=new ArrayList<>();
        users.add(user);
        users.add(user);
        users.add(user);
        model.addAttribute("users",users);
        return "sueecss";
    }
    @RequestMapping("/ajaxTest.do")
    public String ajax(User user,String v_Code,HttpServletRequest request) {
//        List<User> users = userService.query();
//        System.out.println(users.size());
        Object code1=request.getSession().getAttribute("code");
        if(code1!=null) {
            String code = String.valueOf(code1);
            System.out.println(v_Code);
            if (v_Code != null && v_Code != "" && ((v_Code.equals(request.getSession().getAttribute("code"))))) {
//            for (int i = 0; i < users.size(); i++) {
//                if (((users.get(i).getUsername()).equals(user.getUsername())) && ((users.get(i).getPassword()).equals(user.getPassword())) && ((users.get(i).getEmail()).equals(user.getEmail()))) {
                if ((("admin").equals(user.getUsername()))) {
                    return "sueecss";
                } else {
                    return "error";
                }
            }
        }

        return "error";
    }
    @ResponseBody//文本
    @RequestMapping("/ajaxPlay.do")
    public String play(HttpServletRequest request) throws Exception {
        String code = Email.play();
        request.getSession().setAttribute("code",code);
        return code;
    }
}
